﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "StructureInstanceBuildingStoreyData.h"
#include "DbObjectUtils.h"
#include "GbmpBuiltInParameterDefinitionsNew.h"
#include "ICopyStrategyReporter.h"
#include "IDocument.h"
#include "IElement.h"
#include "IElementParentReporter.h"
#include "IExternalDataComponent.h"
#include "IInstance.h"
#include "IParameter.h"
#include "IParameterDefinitionLibrary.h"
#include "IRegenerator.h"
#include "IRegeneratorDataIdCreator.h"

#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"
using namespace gcmp;

IMPLEMENT_REGEN_FUNCTIONS(gcmp, StructureInstanceBuildingStoreyData, BuildingStoreyId)
DBOBJECT_DATA_DEFINE(StructureInstanceBuildingStoreyData)
{
    m_pOwnerElement = nullptr;
}

StructureInstanceBuildingStoreyData::StructureInstanceBuildingStoreyData(IInstance* pInstance, ElementId buildingStoreyId)
    : m_pOwnerElement(pInstance)
    , m_BuildingStoreyId(buildingStoreyId)
{
}

StructureInstanceBuildingStoreyData* StructureInstanceBuildingStoreyData::Create(IInstance* pInstance, ElementId buildingStoreyId)
{
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pInstance, L"pInstance为空",L"GDMPLab",L"2024-03-30");

    OwnerPtr<StructureInstanceBuildingStoreyData> opData = NEW_AS_OWNER_PTR(StructureInstanceBuildingStoreyData, pInstance, buildingStoreyId);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(opData, L"opData为空",L"GDMPLab",L"2024-03-30");

    StructureInstanceBuildingStoreyData* pData = opData.get();
    if (IExternalDataComponent * pExternalDataComponent = pInstance->GetExternalDataComponent())
    {
        pExternalDataComponent->RegisterExternalData(StructureInstanceBuildingStoreyData::GetStaticClassSchema()->GetName(), TransferOwnership(opData));
    }
    return pData;
}

StructureInstanceBuildingStoreyData* StructureInstanceBuildingStoreyData::Get(IInstance* pInstance)
{
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pInstance, L"pInstance为空",L"GDMPLab",L"2024-03-30");
    if (IExternalDataComponent * pExternalDataComponent = pInstance->GetExternalDataComponent())
    {
        IExternalData* pData = pExternalDataComponent->FindExternalData(StructureInstanceBuildingStoreyData::GetStaticClassSchema()->GetName());
        if (!pData)
            return nullptr;

        return quick_cast<StructureInstanceBuildingStoreyData>(pData);
    }
    return nullptr;
}

const StructureInstanceBuildingStoreyData* StructureInstanceBuildingStoreyData::Get(const IInstance* pInstance)
{
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pInstance, L"pInstance为空",L"GDMPLab",L"2024-03-30");
    if (const IExternalDataComponent * pExternalDataComponent = pInstance->GetExternalDataComponent())
    {
        const IExternalData* pData = pExternalDataComponent->FindExternalData(StructureInstanceBuildingStoreyData::GetStaticClassSchema()->GetName());
        if (!pData)
            return nullptr;

        return quick_cast<const StructureInstanceBuildingStoreyData>(pData);
    }
    return nullptr;
}

void StructureInstanceBuildingStoreyData::UpdateForWeakParentDeletion(const std::set<ElementId>& deletedElementIds)
{
    FOR_EACH (deletedElementId, deletedElementIds)
    {
        if (deletedElementId == GetBuildingStoreyId__())
        {
            SetBuildingStoreyId(ElementId::InvalidID);
            break;
        }
    }
}

void StructureInstanceBuildingStoreyData::ReportParents(IElementParentReporter& reporter) const
{
    reporter.ReportStrong(GetBuildingStoreyId__());
}

void StructureInstanceBuildingStoreyData::GetCalculators(ICalculatorCollection * calculators) const
{

}

void StructureInstanceBuildingStoreyData::ReportParameterDefinitions(std::vector<int>* pParamDefIds) const
{
    pParamDefIds->push_back(PARAMETER_ID(BuildingStoreyBuiltInParameter));
}

OwnerPtr<IParameter> StructureInstanceBuildingStoreyData::GetNativeParameter(int paramDefId) const
{
    if (paramDefId == PARAMETER_ID(BuildingStoreyBuiltInParameter))
    {
        //OwnerPtr<IParameter> param = NEW_AS_OWNER_PTR(Parameter, GetDocument(),
            //ParameterStorageType::ElementId, false, false, true,
            //PARAMETER_UID(BuildingStoreyBuiltInParameter), ParameterProcessType::GeneralInput);
        ParameterAttributes pa = {false, false, true};
        OwnerPtr<IParameter> param = IParameter::CreateParameter(GetDocument(), 
            ParameterStorageType::ElementId, pa, PARAMETER_UID(BuildingStoreyBuiltInParameter), 
            ParameterProcessType::GeneralInput);
        param->SetValueAsElementId(GetBuildingStoreyId__());
        return TransferOwnership(param);
    }

    return nullptr;
}

bool StructureInstanceBuildingStoreyData::SetNativeParameter(const IParameter *param, 
    std::wstring* errorMsg/* = nullptr*/)
{
    DBG_WARN_AND_RETURN_FALSE_UNLESS(param, L"param is null.",L"GDMPLab",L"2024-03-30");

    if (param->GetParameterDefinitionId() == PARAMETER_ID(BuildingStoreyBuiltInParameter))
    {
        SetBuildingStoreyId(param->GetValueAsElementId());
        return true;
    }

    return false;
}

bool StructureInstanceBuildingStoreyData::IsParameterModifiable(int paramDefId) const
{
    return true;
}

void StructureInstanceBuildingStoreyData::Report(const ICopyContext & copyContext, ICopyStrategyReporter& reporter) const
{
    reporter.ReportToCopy(GetOwnerElementId());
    reporter.ReportToKeep(GetBuildingStoreyId__());
}

bool StructureInstanceBuildingStoreyData::SetOwnerElement(IElement* pOwnerElement)
{
    m_pOwnerElement = pOwnerElement;
    return true;
}

IElement* StructureInstanceBuildingStoreyData::GetOwnerElement()
{
    return m_pOwnerElement;
}

const IElement* StructureInstanceBuildingStoreyData::GetOwnerElement() const
{
    return m_pOwnerElement;
}

IDocument* StructureInstanceBuildingStoreyData::GetDocument() const
{
    return m_pOwnerElement ? m_pOwnerElement->GetDocument() : nullptr;
}

ElementId StructureInstanceBuildingStoreyData::GetOwnerElementId() const
{
    return m_pOwnerElement ? m_pOwnerElement->GetElementId() : ElementId::InvalidID;
}

NdbObject* StructureInstanceBuildingStoreyData::GetTopOwnerObject() const
{
    if (!GetDocument())
        return nullptr;

    return quick_cast<NdbObject>(GetDocument()->GetElement(GetOwnerElementId()));
}
